package com.jiayi.redisdemo.biz;

import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.StringRedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Function;
import java.util.stream.Collectors;


@Slf4j
@Service
public class DemoService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    public Map<String, String> test1(List<String> keys){
        if(CollectionUtils.isEmpty(keys)){
            return Maps.newHashMap();
        }
        List<String> results = redisTemplate.executePipelined(new RedisCallback<Object>() {
            public Object doInRedis(RedisConnection connection) throws DataAccessException {
                StringRedisConnection stringRedisConn = (StringRedisConnection) connection;
                for (String key : keys) {
                    stringRedisConn.get(key);
                }
                return null;
            }
        }).stream().map(it -> (String) it).collect(Collectors.toList());
        Map<String, String> map = Maps.newHashMap();
        for (String key : keys) {
            String value = results.get(keys.indexOf(key));
            map.put(key, value);
        }
        return map;
    }

    public void test2(List<String> keys){
        keys.forEach(it -> redisTemplate.opsForValue().set(it ,it.toUpperCase()));
    }
}
